package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.dto.DtreeSqlDto;
import com.ys.entity.sys.YsButton;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.sys.YsMenuEnum;
import com.ys.mapper.sys.SysMapper;
import com.ys.mapper.sys.YsButtonMapper;
import com.ys.service.sys.YsButtonService;
import com.ys.vo.R;

@Service("ysButtonService")
public class YsButtonServiceImpl implements YsButtonService {

	@Autowired
	private SysMapper sysMapper;
	@Autowired
	private YsButtonMapper ysButtonMapper;

	@Override
	@Transactional
	public R deleteTrue(Long id) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		ysButtonMapper.deleteTrue(id);
		
		// 删除角色-按钮
		sysMapper.deleteTrueYsRoleButtonByButton(id);
		
		return R.ok();
	}

	@Override
	public DtreeSqlDto dtree() {
		
		DtreeSqlDto.StatusDto status = DtreeSqlDto.StatusDto.init(200, "");

		List<DtreeSqlDto.DataDto> data = new ArrayList<DtreeSqlDto.DataDto>();
		List<YsMenu> listYsMenu = new YsMenu()
				.setOrderby("A.Lev asc, A.SortNo asc")
				.queryList();
		
		// 一级菜单
		for(YsMenu ysMenu1 : listYsMenu) {
			if (ysMenu1.getLev() != YsMenuEnum.Lev.ONE.value) {
				continue;
			}
			String id1 = ysMenu1.getId().toString();
			String title1 = ysMenu1.getName();
			String parentId1 = ysMenu1.getPid().toString();
			
			// 二级菜单
			List<DtreeSqlDto.DataDto> children = new ArrayList<DtreeSqlDto.DataDto>();
			for (YsMenu ysMenu2 : listYsMenu) {
				if (ysMenu2.getPid().longValue() != ysMenu1.getId().longValue()) {
					continue;
				}
				String id2 = ysMenu2.getId().toString();
				String title2 = ysMenu2.getName();
				String parentId2 = ysMenu2.getPid().toString();
				
				// 三级菜单
				List<DtreeSqlDto.DataDto> children1 = new ArrayList<DtreeSqlDto.DataDto>();
				for (YsMenu ysMenu3 : listYsMenu) {
					if (ysMenu3.getPid().longValue() != ysMenu2.getId().longValue()) {
						continue;
					}
					String id3 = ysMenu3.getId().toString();
					String title3 = ysMenu3.getName();
					String parentId3 = ysMenu3.getPid().toString();
					List<DtreeSqlDto.DataDto> children2 = null;
					DtreeSqlDto.DataDto dto = new DtreeSqlDto.DataDto(id3, title3, parentId3, DtreeSqlDto.DataDto.initCheckArr(null), children2);
					children1.add(dto);
				}
				
				DtreeSqlDto.DataDto dataDto2 = new DtreeSqlDto.DataDto(id2, title2, parentId2, DtreeSqlDto.DataDto.initCheckArr(null), children1);
				children.add(dataDto2);
			}
			
			DtreeSqlDto.DataDto dataDto1 = new DtreeSqlDto.DataDto(id1, title1, parentId1, DtreeSqlDto.DataDto.initCheckArr(null), children);
			data.add(dataDto1);
		}
		return new DtreeSqlDto(status, data);
	}

	@Override
	public R add(Long menuId,
			Integer type,
			Integer styleType,
			Integer sortNo,
			String event,
			String name,
			String icon,
			String nameShiroAuth) {
		
		if (styleType == null) {
			return R.error("按钮样子为空");
		}
		
		String remark = null;
		YsButton entity = YsButton.ysButton(menuId, type, styleType, name, event, icon, nameShiroAuth, sortNo, remark, 1);
		
		ysButtonMapper.insert(entity);
		
		return R.ok();
	}

	@Override
	public R edit(Long id,
			Integer type,
			Integer styleType,
			String name,
			String event,
			String icon,
			String nameShiroAuth) {
		
		YsButton entity = new YsButton().setId(id).queryFirst();
		entity
			.setType(type)
			.setStyleType(styleType)
			.setName(name)
			.setEvent(event)
			.setIcon(icon)
			.setNameShiroAuth(nameShiroAuth);
		
		ysButtonMapper.update(entity);
		
		return R.ok();
	}
}
